package sg.edu.nus.iss.billing.gui;

//Tan Pic Cheng

import java.awt.Choice;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import sg.edu.nus.iss.billing.entity.Customer;
import sg.edu.nus.iss.billing.entity.RecordState;
import sg.edu.nus.iss.billing.gui.component.ValidateJTextField;
import sg.edu.nus.iss.billing.localization.MessageKeyManager;
import sg.edu.nus.iss.billing.validation.ValidationRule;

public class UpdateCustomerFieldPanel extends JPanel {
	
	private String[] labelContent = {MessageKeyManager.getMessage("label.status"), MessageKeyManager.getMessage("label.accountNo"), MessageKeyManager.getMessage("label.NRIC/FIN"), MessageKeyManager.getMessage("label.saluation"), MessageKeyManager.getMessage("label.name"), MessageKeyManager.getMessage("label.emailAdd")};
		
	private JLabel lblCustHeader;
	private JLabel lblCustStatus;
	private JLabel lblCustID;
	private JLabel lblShowCustID;
	private JLabel lblCustNRIC;
	private JLabel lblShowCustNRIC;
	private JLabel lblShowCustStatus;
	private JLabel lblSaluation;
	private JLabel lblName;
	private JLabel lblEmail;
		
	private ValidateJTextField fieldName;
	private ValidateJTextField fieldEmail;
	private Choice saluation;
	
	private int labelHeight = 20;
	private int fieldHeight = 20;
	private int choiceHeight = 20;
	private int labelWidth = 120;
	private int fieldWidth = 200;
	private int choiceWidth = 200;
	private int fieldControlX = 150;
	private int lblControlX = 10; 
	
	private Customer customer;
			
	public UpdateCustomerFieldPanel(Customer customer) {
		// TODO Auto-generated constructor stub
		super();
		init();
		this.customer = customer;
		/* No need validation as it is expected to be validated at the first place */
		lblShowCustNRIC.setText(customer.getNRIC());
		lblShowCustID.setText(customer.getAccountNumber());	
		saluation.select(customer.getSalutation());
		fieldName.setText(customer.getName());
		fieldEmail.setText(customer.getEmail());	
		
		if(customer.getActive()== RecordState.ACTIVE)
			lblShowCustStatus.setText("ACTIVE");
		}
	private JLabel createLabel(String labelContents, int x, int y){
		
		JLabel labelName = new JLabel(labelContents);
		labelName.setVerticalTextPosition(SwingConstants.BOTTOM);
		labelName.setBounds(x, y, labelWidth, labelHeight);
		add(labelName);
		
		return labelName;		
		
	}
	private ValidateJTextField createTextField(String labelName, String text, int x, int y, ValidationRule ... rules){
		
		ValidateJTextField fieldTxtName = new ValidateJTextField(labelName, rules);
		fieldTxtName.setText(text);
		fieldTxtName.setBounds(x, y, fieldWidth, fieldHeight);
		add(fieldTxtName);	
		
		return fieldTxtName;
		
	}
	private void createSaluation(int y){
		
		saluation = new Choice();
		saluation.addItem("Mr");
		saluation.addItem("Miss");
		saluation.addItem("Mdm");
		saluation.addItem("Dr");
		saluation.setBounds(fieldControlX, y, choiceWidth, choiceHeight);
		
		add(saluation);
		
	}
	public ValidateJTextField getFieldName() {
		
		return fieldName;
		
	}
	public ValidateJTextField getFieldEmail() {
		
		return fieldEmail;
	}
	public Choice getSaluation() {
		
		return saluation;
		
	}
	public void setFieldName(ValidateJTextField fieldName) {
		
		this.fieldName = fieldName;
		
	}
	public void setFieldEmail(ValidateJTextField fieldEmail) {
		
		this.fieldEmail = fieldEmail;
		
	}		
	public long getLongCustID() {
		
		long accountNo = Long.parseLong(lblShowCustID.getText());
		
		return accountNo;
	}
	public String getStringCustAcctID(){
		
		return lblShowCustID.getText();
		
	}
	public String getCustStatus(){
		if(customer.getActive()== RecordState.ACTIVE)
			return "ACTIVE";
		else
			return "DELETED";		
	}
	private void init() {
		// TODO Auto-generated method stub
		setLayout(null);
		setPreferredSize(new Dimension(400, 300));
		setBounds(0, 0, 400, 300);
				
		lblCustHeader = createLabel("Customer Details:", 10, 5);
		
		int controly = 40;
		
		
		
		for(int i = 0; i < labelContent.length; i++){
				
			switch(i){
			case 0: lblCustStatus = createLabel(labelContent[i], lblControlX, controly);
					//set new customer status as ACTIVE
					lblShowCustStatus = createLabel("", fieldControlX, controly);
					break;
			case 1: lblCustID =	createLabel(labelContent[i], lblControlX, controly);
					lblShowCustID = createLabel("", fieldControlX, controly);	
					break;
			case 2:	lblCustNRIC = createLabel(labelContent[i], lblControlX, controly);
					lblShowCustNRIC = createLabel("", fieldControlX, controly);
					break;
			case 3:	lblSaluation = createLabel(labelContent[i], lblControlX, controly);
					createSaluation(controly);
					break;
			case 4: lblName = createLabel(labelContent[i], lblControlX, controly);
					fieldName = createTextField(labelContent[i], " ", fieldControlX, controly, ValidationRule.NOT_NULL, ValidationRule.ALPHANUMERIC);
					break;
			case 5:	lblEmail = createLabel(labelContent[i], lblControlX, controly);
					fieldEmail = createTextField(labelContent[i], " ", fieldControlX, controly, ValidationRule.EMAIL);
					break;
			default:break;
			}//End of Switch
			
			controly= controly + 35;
			
		}
		
	}
							
}


